//package com.example.xhandroidexample.systemapi.pictureinpicture;
//
//import android.annotation.SuppressLint;
//import android.content.Intent;
//import android.content.pm.ActivityInfo;
//import android.media.MediaPlayer;
//import android.os.Handler;
//import android.os.Message;
//import android.util.Log;
//import android.view.SurfaceHolder;
//import android.view.SurfaceView;
//import android.view.View;
//import android.widget.Button;
//import android.widget.LinearLayout;
//import android.widget.SeekBar;
//import android.widget.TextView;
//
//import androidx.annotation.NonNull;
//
//import com.example.lib.component.BaseActivity;
//import com.example.router.ActivityRouter;
//import com.example.xhandroidexample.R;
//
//@ActivityRouter(category = "系统API",title = "画中画模式",descript = "画中画模式的测试实验")
//public class PictureInPictureActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener {
//
//    private SurfaceView sfvVideo;
//    private static final String TAG = "PictureInPictureActivity";
//    private MediaPlayer mMediaPlayer;
//    private Button btPlay;
//    private static final int GET_DURRENT_POSITION = 1;
//    private static final int ONE_SECOND = 1000;
//
//
//    @SuppressLint("HandlerLeak")
//    Handler bgHandler = new Handler() {
//
//        @SuppressLint({"LongLogTag", "SetTextI18n"})
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//
//            if (msg.what == GET_DURRENT_POSITION) {
//                int currentPosition = mMediaPlayer.getCurrentPosition();
//                Log.d(TAG, "handleMessage: currentPosition = " + currentPosition);
//                tvCurrentTime.setText(currentPosition / 1000 + "");
//                skb_progress.setProgress(currentPosition / 1000);
//                bgHandler.sendEmptyMessageDelayed(GET_DURRENT_POSITION, ONE_SECOND);
//            }
//        }
//    };
//    private TextView tvTotalTime;
//    private TextView tvCurrentTime;
//    private SeekBar skb_progress;
//
//    @SuppressLint("LongLogTag")
//    @Override
//    protected void initOnCreate() {
//        sfvVideo = findViewById(R.id.sfv_video);
//        SurfaceHolder surfaceholder = sfvVideo.getHolder();
//        surfaceholder.setKeepScreenOn(true);
//
//
//        btPlay = findViewById(R.id.bt_play);
//        tvTotalTime = findViewById(R.id.tv_total_time);
//        tvCurrentTime = findViewById(R.id.tv_current_time);
//        btPlay.setEnabled(false);
//
//        skb_progress = findViewById(R.id.skb_progress);
//        skb_progress.setOnSeekBarChangeListener(this);
//
//        surfaceholder.addCallback(new SurfaceHolder.Callback() {
//
//            @Override
//            public void surfaceCreated(SurfaceHolder holder) {
//                Log.d(TAG, "surfaceCreated: ");
////                AssetFileDescriptor fd = getResources().openRawResourceFd(R.raw.vid_bigbuckbunny);
//                //                try {
////                    mMediaPlayer.setDataSource(fd);
////                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
////                        mMediaPlayer.setDataSource(fd);
////                    }
////                } catch (Exception e) {
////                    e.printStackTrace();
////                }
//                mMediaPlayer = MediaPlayer.create(PictureInPictureActivity.this,R.raw.vid_bigbuckbunny);
//
//
//                mMediaPlayer.setLooping(false);
//                // 把视频画面输出到SurfaceView
//                mMediaPlayer.setDisplay(holder);
//                // 通过异步的方式装载媒体资源
////                mMediaPlayer.prepareAsync();
//
//                mMediaPlayer.setOnBufferingUpdateListener((mp, percent) -> Log.d(TAG, "onBufferingUpdate: percent = " + percent));
//
//                mMediaPlayer.setOnInfoListener((mp, what, extra) -> {
//                    Log.d(TAG, "onInfo: what = " + what + " extra = " + extra);
//                    return false;
//                });
//
//                mMediaPlayer.setOnPreparedListener(mp -> {
//                    //装载完毕回调
//                    Log.d(TAG, "onPrepared: ");
//                    btPlay.setEnabled(true);
//                    initVideoParams();
//                });
//
//                mMediaPlayer.setOnVideoSizeChangedListener((mp, width, height) -> {
//                    Log.d(TAG, "onVideoSizeChanged: ");
//                    changeVideoSize();
//                });
//
//                mMediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
//                    @Override
//                    public void onSeekComplete(MediaPlayer mp) {
//                        Log.d(TAG, "onSeekComplete: ");
//                    }
//                });
//
//                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                    @Override
//                    public void onCompletion(MediaPlayer mp) {
//                        Log.d(TAG, "onCompletion: ");
////                        mMediaPlayer.stop();
//                        btPlay.setText("播放");
//                        bgHandler.removeMessages(GET_DURRENT_POSITION);
//                    }
//                });
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//                Log.d(TAG, "surfaceChanged: width = " + width + " height = " + height);
//            }
//
//            @Override
//            public void surfaceDestroyed(SurfaceHolder holder) {
//
//            }
//        });
//    }
//
//
//
//    @SuppressLint({"LongLogTag", "SetTextI18n"})
//    private void initVideoParams() {
//        int videoDuration = mMediaPlayer.getDuration();
//        tvTotalTime.setText(videoDuration/1000+"");
//        skb_progress.setMax(videoDuration/1000);
//        Log.d(TAG, "initVideoParams: videoDuration = " + videoDuration);
//
//    }
//
//    public void play(View view) {
//        if (judgeVideoIsPlaying()) {
//            pauseVideo();
//        } else {
//            playVideo();
//        }
//    }
//
//    public void intoPIP(View view) {
//        Intent intent = new Intent(this,PictureInPictureActivity2.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (mMediaPlayer != null) {
//            mMediaPlayer.release();
//        }
//        bgHandler.removeMessages(GET_DURRENT_POSITION);
//    }
//
//    /**
//     * 暂停
//     */
//    private void pauseVideo() {
//        if (mMediaPlayer != null) {
//            btPlay.setText("播放");
//            bgHandler.removeMessages(GET_DURRENT_POSITION);
//            mMediaPlayer.pause();
//        }
//    }
//
//    @SuppressLint("LongLogTag")
//    private boolean judgeVideoIsPlaying() {
//        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
//            Log.d(TAG, "playVideo: mMediaPlayer.isPlaying() = " + mMediaPlayer.isPlaying());
//            return true;
//        } else {
//            return false;
//        }
//    }
//
//    /**
//     * 播放
//     */
//    @SuppressLint("LongLogTag")
//    private void playVideo() {
//        if (mMediaPlayer != null) {
//            btPlay.setText("暂停");
//            bgHandler.sendEmptyMessageDelayed(GET_DURRENT_POSITION,ONE_SECOND);
//            mMediaPlayer.start();
//        }
//    }
//
//    //改变视频的尺寸自适应。
//    @SuppressLint("LongLogTag")
//    public void changeVideoSize() {
//        int videoWidth = mMediaPlayer.getVideoWidth();
//        int videoHeight = mMediaPlayer.getVideoHeight();
//        Log.d(TAG, "changeVideoSize: videoWidth = " + videoWidth + " videoHeight = " + videoHeight);
//
//        int surfaceWidth = sfvVideo.getWidth();
//        int surfaceHeight = sfvVideo.getHeight();
//        Log.d(TAG, "changeVideoSize: surfaceWidth = " + surfaceWidth + " surfaceHeight = " + surfaceHeight);
//
//        //根据视频尺寸去计算->视频可以在sufaceView中放大的最大倍数。
//        float max;
//        if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//            //竖屏模式下按视频宽度计算放大倍数值
//            max = (float) videoWidth / (float) surfaceWidth;
//        } else {
//            //横屏模式下按视频高度计算放大倍数值
//            max = (float) videoWidth / (float) surfaceHeight;
//        }
//
//        //视频宽高分别/最大倍数值 计算出放大后的视频尺寸
//        videoWidth = (int) Math.ceil((float) videoWidth / max);
//        videoHeight = (int) Math.ceil((float) videoHeight / max);
//
//        Log.d(TAG, "changeVideoSize: change videoWidth = " + videoWidth + " videoHeight = " + videoHeight);
//        //无法直接设置视频尺寸，将计算出的视频尺寸设置到surfaceView 让视频自动填充。
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(videoWidth, videoHeight);
//        sfvVideo.setLayoutParams(params);
//        sfvVideo.invalidate();
//    }
//
//    @Override
//    protected int setLayoutXmlid() {
//        return R.layout.activity_picture_in_picture;
//    }
//
//    @Override
//    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//    }
//
//    @Override
//    public void onStartTrackingTouch(SeekBar seekBar) {
//        pauseVideo();
//    }
//
//    @SuppressLint("LongLogTag")
//    @Override
//    public void onStopTrackingTouch(SeekBar seekBar) {
//        int seekBarProgress = seekBar.getProgress();
//        Log.d(TAG, "onStopTrackingTouch: seekBarProgress = " + seekBarProgress);
//        mMediaPlayer.seekTo(seekBarProgress*ONE_SECOND);
//        playVideo();
//    }
//}
